package com.nbjtjc.safe.service.sys;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.mapper.sys.SysDutyMapper;
import com.nbjtjc.safe.mapper.sys.SysDutyPublicMapper;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.model.sys.SysDuty;
import com.nbjtjc.safe.model.sys.SysDutyPublic;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.util.DateUtils;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 系统值班
 * @auther guqianbin
 * @date 2018/7/23 9:59
 **/
@Service
public class SysDutyPublicService {
    @Autowired
    private SysDutyPublicMapper sysDutyPublicMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysDutyMapper sysDutyMapper;

    @Transactional
    public SysDutyPublic add(SysDutyPublic sysDutyPublic){
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        sysDutyPublic.setCompanyId(MyShiroUtils.getCompanyId());
        sysDutyPublic.setDeptId(MyShiroUtils.getDeptId());
        sysDutyPublic.setCompanyName(sysUser.getCompanyName());
        sysDutyPublic.setDeptName(sysUser.getDeptName());
        sysDutyPublic.setUserId(MyShiroUtils.getSessionUserId());
        sysDutyPublic.setUsername(MyShiroUtils.getSessionUserName());
        Date startDate=sysDutyPublic.getStartDate();
        List<Date> dutyDates=DateUtils.getDateRange(startDate,sysDutyPublic.getDutyDayCount());
        sysDutyPublicMapper.insertSelective(sysDutyPublic);

        List<SysDept> subCompanies= sysDeptMapper.findOnlyDeptByParentId(MyShiroUtils.getCompanyId()
                ,Constant.SysDeptType.COMPANY.getValue());
        SysDept sysDept=sysDeptMapper.selectByPrimaryKey(MyShiroUtils.getCompanyId());
        if(CollectionUtil.isEmpty(subCompanies)){
            subCompanies=new ArrayList<>();
        }
        subCompanies.add(sysDept);
        List<SysDuty> sysDuties=new ArrayList<>();
        subCompanies.forEach(company->{
            for(int i=0;i<dutyDates.size();i++){
                SysDuty sysDuty=new SysDuty();
                sysDuty.setDutyDate(dutyDates.get(i));
                sysDuty.setCompanyName(company.getName());
                sysDuty.setCompanyId(company.getId());
                sysDuty.setDutyPublicId(sysDutyPublic.getId());
                sysDuties.add(sysDuty);
            }
        });
        sysDutyMapper.insertList(sysDuties);
        sysDutyPublic.setSysDuties(sysDuties);
        return sysDutyPublic;
    }

    public PageInfo find(SysDutyPublic sysDutyPublic){
        PageHelper.startPage(sysDutyPublic.getPageCurrent(),sysDutyPublic.getPageSize());
        List<SysDutyPublic> sysDutyPublics=sysDutyPublicMapper.search(sysDutyPublic);
        return new PageInfo(sysDutyPublics);
    }
}
